package org.jeecg.modules.wlhy.mbshipping.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.daikai.mabang.dto.MbVehicleDTO;
import org.jeecg.modules.third.daikai.mabang.service.MaBangApiService;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.mbshipping.entity.HyMbVehicle;
import org.jeecg.modules.wlhy.mbshipping.mapper.HyMbVehicleMapper;
import org.jeecg.modules.wlhy.mbshipping.service.IHyMbVehicleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description: hy_mb_vehicle
 * @Author: duanlikao
 * @Date:   2022-12-19
 * @Version: V1.0
 */
@Service
public class HyMbVehicleServiceImpl extends ServiceImpl<HyMbVehicleMapper, HyMbVehicle> implements IHyMbVehicleService {




    @Resource
    private MaBangApiService maBangApiService;


    @Resource
    private IHyVehicleService hyVehicleService;


    @Override
    public boolean uploadVehicle(String vehicleId){
        HyVehicle vehicle = hyVehicleService.getById(vehicleId);
        if(vehicle == null){
            throw new JeecgBootException("该车辆上报信息不能为空");
        }

        MbVehicleDTO paramDTO =  this.vehicleTOMbVehicleDTO(vehicle) ;

        String res = maBangApiService.vehiclePush( paramDTO);

        HyMbVehicle hyMbVehicle = new HyMbVehicle();
        hyMbVehicle.setVehicleId(vehicleId);
        hyMbVehicle.setUploadTime( new Date());

        if(StringUtils.isBlank(res)){
            hyMbVehicle.setUploadStatus("2");
            hyMbVehicle.setUploadFailureReason("推送异常");
            this.saveOrUpdateMbVehicle(hyMbVehicle);
            throw new JeecgBootException("该车辆推送异常！");
        }else{
            Result r = JSONObject.parseObject(res,Result.class);

            if(500 == r.getCode()){
                if(r.getMessage().contains("已存在")){
                    hyMbVehicle.setUploadStatus("1");
                    this.saveOrUpdateMbVehicle(hyMbVehicle);
                    return true;
                }
            }

            if (r.isSuccess() && 200 == r.getCode()) {
                hyMbVehicle.setUploadStatus("1");
                this.saveOrUpdateMbVehicle(hyMbVehicle);
                return true;
            }else{
                hyMbVehicle.setUploadStatus("2");
                hyMbVehicle.setUploadFailureReason(r.getMessage());
                this.saveOrUpdateMbVehicle(hyMbVehicle);
                throw new JeecgBootException("该车辆推送异常" +r.getMessage());
            }
        }
    }

    public  MbVehicleDTO vehicleTOMbVehicleDTO(HyVehicle vehicle) {
        if (vehicle == null) {
            return null;
        }
        MbVehicleDTO mbVehicleDTO = new MbVehicleDTO();

        mbVehicleDTO.setVehicleNumber(vehicle.getVehicleNumber());
        mbVehicleDTO.setVehiclePlateColorCode(vehicle.getVehiclePlateColorCode());

        mbVehicleDTO.setVehicleType(vehicle.getVehicleType());
        mbVehicleDTO.setVehicleLong(vehicle.getVehicleLong());
        mbVehicleDTO.setVehicleWidth(vehicle.getVehicleWidth());
        mbVehicleDTO.setVehicleHeight(vehicle.getVehicleHeight());


        mbVehicleDTO.setDrivingLicenseNum(vehicle.getDrivingLicenseNum());
        mbVehicleDTO.setDrivingLicenseImg(
                maBangApiService.uploadFiles(  vehicle.getDrivingLicenseImg(), "车辆")
        );
        mbVehicleDTO.setDrivingLicenseImg2(
                maBangApiService.uploadFiles(  vehicle.getDrivingLicenseImg2(), "车辆")
        );
        mbVehicleDTO.setBrandName(vehicle.getBrandName());
        mbVehicleDTO.setOwner(vehicle.getOwner());
        mbVehicleDTO.setUseCharacter(vehicle.getUseCharacter());
        mbVehicleDTO.setVin(vehicle.getVin());
       // mbVehicleDTO.setCreditCode(vehicle.getCreditCode());
        mbVehicleDTO.setIssuingOrganizations(vehicle.getIssuingOrganizations());
        mbVehicleDTO.setRegisterDate(
                vehicle.getRegisterDate().getTime()
        );
        mbVehicleDTO.setIssueDate(
                vehicle.getIssueDate().getTime()
        );
        mbVehicleDTO.setVehicleEnergyType(vehicle.getVehicleEnergyType());
        mbVehicleDTO.setVehicleTonnage(vehicle.getVehicleTonnage().intValue());
        mbVehicleDTO.setGrossMass(vehicle.getGrossMass().intValue());
        mbVehicleDTO.setRoadTransportNumber(vehicle.getRoadTransportNumber());
        mbVehicleDTO.setRoadTransportImg(
                maBangApiService.uploadFiles(  vehicle.getRoadTransportImg(), "车辆")
        );
        //mbVehicleDTO.setRoadTransportImg2(
        //        maBangApiService.uploadFiles(   vehicle.getRoadTransportImg2())
        //);
        if(vehicle.getRoadTransportExpiredDate() != null){
            mbVehicleDTO.setRoadTransportExpiredDate(
                    vehicle.getRoadTransportExpiredDate().getTime()
            );
        }

      //  mbVehicleDTO.setBusinessLicense(vehicle.getBusinessLicense());
        mbVehicleDTO.setTrailerVehiclePlateNumber(vehicle.getTrailerVehiclePlateNumber());
        mbVehicleDTO.setIzAffiliation(vehicle.getIzAffiliation());
      //  mbVehicleDTO.setAffiliationAgreement(vehicle.getAffiliationAgreement());

        mbVehicleDTO.setVehicleImg(vehicle.getVehicleImg());
       // mbVehicleDTO.setRegistCertNo(vehicle.getRegistCertNo());
        mbVehicleDTO.setVehicleAddress(vehicle.getVehicleAddress());
       // mbVehicleDTO.setTrailerVehiclePlateColorCode(vehicle.getTrailerVehiclePlateColorCode());
        mbVehicleDTO.setRoadTransportName(vehicle.getRoadTransportName());


        mbVehicleDTO.setEmissionStandardType(vehicle.getEmissionStandardType());
        //mbVehicleDTO.setEnvironmentalCertificate(vehicle.getEnvironmentalCertificate());
        //
        //mbVehicleDTO.setTrailerVehiclLicenseImg(vehicle.getTrailerVehiclLicenseImg());
        //mbVehicleDTO.setTrailerVehiclLicenseImg2(vehicle.getTrailerVehiclLicenseImg2());
        return mbVehicleDTO;
    }






    @Override
    public boolean saveOrUpdateMbVehicle(HyMbVehicle hyMbVehicle ){
        try {
            hyMbVehicle.setId(hyMbVehicle.getVehicleId());
            return this.saveOrUpdate(hyMbVehicle);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return false;
    }

}
